package com.drugStorage.struts.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.drugStorage.domain.User;
import com.drugStorage.spring.service.UserService;
import com.drugStorage.struts.actionForms.UserActionForm;

public class UserAction extends DispatchAction {
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		UserService userService = (UserService) factory.getBean("UserService");
		List<User> userlist = userService.getAllUsers();
		System.out.println(userlist);
		request.setAttribute("userlist", userlist);
		return mapping.findForward("list_success");
	}

	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		UserActionForm UAF = (UserActionForm) form;
		String name = UAF.getName();
		String userType = UAF.getUserType();
		String password1 = UAF.getPassword1();
		String password2 = UAF.getPassword2();
		String sex = UAF.getSex();
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxx");
		if (password1.equals(password2))

		{
			// ��ActionForm�������Դ��ݽ���
			User user = new User();
			user.setName(name);
			user.setPassword(password1);
			user.setUserType(userType);
			user.setSex(sex);
			BeanFactory factory = new ClassPathXmlApplicationContext(
					"applicationContext-*.xml");
			UserService userService = (UserService) factory
					.getBean("UserService");

			userService.saveUser(user);
			return mapping.findForward("success");
		} else {
			return mapping.findForward("AddUserFail");

		}
	}

	public ActionForward remove(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		UserService userService = (UserService) factory.getBean("UserService");
		String idName = request.getParameter("id");
		int id = Integer.parseInt(idName);
		// Integer userid = Integer.parseInt(id);
		userService.removeUser(id);
		return mapping.findForward("success");
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("-----------------xxx------------");
		UserActionForm UAF = (UserActionForm) form;
		String name = UAF.getName();
		String userType = UAF.getUserType();
		String password1 = UAF.getPassword1();
		String password2 = UAF.getPassword2();
		String sex = UAF.getSex();
//		System.out.println(sex);
//		System.out.println(id);
		System.out.println("-----------------yyy------------");
		BeanFactory factory = new ClassPathXmlApplicationContext(
		"applicationContext-*.xml");
UserService userService = (UserService) factory
		.getBean("UserService");
	
		String name1 = request.getParameter("name");
		System.out.println(name1);
		User user = userService.findByName(name1);
		System.out.println(user.getId());
		if (password1.equals(password2))

		{
			// ��ActionForm�������Դ��ݽ���
//			user.setId(id);
//			user.setName(name);
			user.setPassword(password1);
			user.setUserType(userType);
			user.setSex(sex);
			
			System.out.println("-----------------zzz------------");
			userService.updateUser(user);
			return mapping.findForward("success");
		} else {
			return mapping.findForward("AddUserFail");

		}
	}

	public ActionForward find(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		UserActionForm uaf = (UserActionForm) form;
		String name = uaf.getName();
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		UserService userService = (UserService) factory
				.getBean("UserService");
		User user = userService.findByName(name);
		request.setAttribute("user", user);
		
		return mapping.findForward("find_success");
	}
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		UserService userService = (UserService) factory.getBean("UserService");
		UserActionForm uaf = (UserActionForm) form;
		String[] userList = uaf.getSelectFlag();
		userService.del(userList);
		List<User> list = userService.getAllUsers();
		
		request.setAttribute("userList", list);
		System.out.println(list.size());
		// Integer userid = Integer.parseInt(id);
		return mapping.findForward("success");
	}


}
